import {defineStore} from 'pinia'
import {cloudGeneralObjectCustomUI} from '@/uniCloud-alipay'

const useOrderStore = defineStore('order', () => {
  //获取订单详情
  const fetchOrderDetailAction = async (order) => {
    if (!order?._id) return

    //获取地址信息
    const address = (await cloudGeneralObjectCustomUI.getListByField('address', '_id', order.address_id)).data[0]

    //查询用户信息
    const userInfo = (await cloudGeneralObjectCustomUI.getListByField('user', '_id', order.user_id)).data[0]

    //联表查询获取农品列表
    const commoditys = (await cloudGeneralObjectCustomUI.getInfoByTwoTable('order-commodities', 'commodity', {
      mainRule: `order_id == "${order._id}"`,
    })).data

    return {userInfo, address, commoditys}
  }

  return {fetchOrderDetailAction}
})

export default useOrderStore
